package com.wdz.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.wdz.annotations.SecureInterception;
import com.wdz.diary.entity.Diary;
import com.wdz.diary.service.IDiaryService;
import com.wdz.http.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/diary")
public class DiaryController {
    @Autowired
    private IDiaryService iDiaryService;

    @PostMapping("/")
    @SecureInterception
    public Response add(@RequestBody Diary diary){iDiaryService.insert(diary); return Response.success((Object)diary.getId() );}

    @GetMapping("/{pageNo}/{pageSize}")
    public Response get(@PathVariable int pageNo,@PathVariable int pageSize){
        Map<String,Object> data = new HashMap<>();
        data.put("total",iDiaryService.selectCount(null));
        data.put("data",iDiaryService.selectPage(new Page<>(pageNo,pageSize),new EntityWrapper<Diary>().orderBy("create_date",false)).getRecords());
        return Response.success(data);
    }

    @DeleteMapping("/{id}")
    public Response delete(@PathVariable int id){iDiaryService.deleteById(id);return Response.success();}

    @GetMapping("/{id}")
    public Response get(@PathVariable String id){return Response.success(iDiaryService.selectById(id));}
}
